﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using RSHB.Framework.Data;

namespace RSHB.Framework.BLL
{
    public class LoanAgreementRtfFileContentBuilder : RTFFileContentBuilder
    {
        IDictionary<string, string> _replaceParamValueDictionary;
        LoanAgreement _loanAgreeement;

        public LoanAgreementRtfFileContentBuilder(int loanAgreementID)
        {
            this._loanAgreeement = (new BHDDataContext()).LoanAgreements.SingleOrDefault(l => l.LoanAgreementID == loanAgreementID);
            if (this._loanAgreeement == null)
            {
                throw new ApplicationException(string.Format("Кредитный договор с ID = {1} не найден", loanAgreementID));
            }


            this._replaceParamValueDictionary = new Dictionary<string, string>();

            VCard coBorrowerVCard = this._loanAgreeement.CoBorrower.VCards.Single();
            this._replaceParamValueDictionary.Add("CoBorrowerNameLast", coBorrowerVCard.vcardNameLast);
            this._replaceParamValueDictionary.Add("CoBorrowerNameFirst", coBorrowerVCard.vcardNameFirst);
            this._replaceParamValueDictionary.Add("CoBorrowerNameMiddle", coBorrowerVCard.vcardNameMiddle);
            this._replaceParamValueDictionary.Add("CoBorrowerPassportSeries", coBorrowerVCard.vcardPassportSeries);
            this._replaceParamValueDictionary.Add("CoBorrowerPassportNum", coBorrowerVCard.vcardPassportNum);
            this._replaceParamValueDictionary.Add("CoBorrowerPassportIssuerName", coBorrowerVCard.vcardPassportIssuerName);
            this._replaceParamValueDictionary.Add("CoBorrowerPassportIssueDateDay", coBorrowerVCard.vcardPassportIssueDate.Value.Day.ToString());
            this._replaceParamValueDictionary.Add("CoBorrowerPassportIssueDateMonth", System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.MonthNames[coBorrowerVCard.vcardPassportIssueDate.Value.Month - 1]);
            this._replaceParamValueDictionary.Add("CoBorrowerPassportIssueDateYear", coBorrowerVCard.vcardPassportIssueDate.Value.Year.ToString());
            this._replaceParamValueDictionary.Add("CoBorrowerPassportIssuerCode", coBorrowerVCard.vcardPassportIssuerCode);
            this._replaceParamValueDictionary.Add("CoBorrowerAddress", coBorrowerVCard.vcardAddressHomeArea);
                        
            VCard borrowerVCard = this._loanAgreeement.Borrower.VCards.Single();
            this._replaceParamValueDictionary.Add("BorrowerNameLast", borrowerVCard.vcardNameLast);
            this._replaceParamValueDictionary.Add("BorrowerNameFirst", borrowerVCard.vcardNameFirst);
            this._replaceParamValueDictionary.Add("BorrowerNameMiddle", borrowerVCard.vcardNameMiddle);
            this._replaceParamValueDictionary.Add("BorrowerPassportSeries", borrowerVCard.vcardPassportSeries);
            this._replaceParamValueDictionary.Add("BorrowerPassportNum", borrowerVCard.vcardPassportNum);
            this._replaceParamValueDictionary.Add("BorrowerPassportIssuerName", borrowerVCard.vcardPassportIssuerName);
            this._replaceParamValueDictionary.Add("BorrowerPassportIssueDateDay", borrowerVCard.vcardPassportIssueDate.Value.Day.ToString());
            this._replaceParamValueDictionary.Add("BorrowerPassportIssueDateMonth", System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.MonthNames[borrowerVCard.vcardPassportIssueDate.Value.Month - 1]);
            this._replaceParamValueDictionary.Add("BorrowerPassportIssueDateYear", borrowerVCard.vcardPassportIssueDate.Value.Year.ToString());
            this._replaceParamValueDictionary.Add("BorrowerPassportIssuerCode", borrowerVCard.vcardPassportIssuerCode);
            this._replaceParamValueDictionary.Add("BorrowerAddress", borrowerVCard.vcardAddressHomeArea);

            
        }

        public LoanAgreementRtfFileContentBuilder(int loanAgreementID, IDictionary<string, string> replaceParamValueDictionary)
            : this(loanAgreementID)
        {
            this._replaceParamValueDictionary = replaceParamValueDictionary;
        }

        protected override void ProcessDocument()
        {
            foreach (var dictionaryItem in this._replaceParamValueDictionary)
            {
                this.rtb.Rtf = Regex.Replace(this.rtb.Rtf, dictionaryItem.Key, dictionaryItem.Value);
            }                        
        }
    }
}
